package io.github.ali.commons.bean;

import org.apache.commons.lang3.builder.ToStringBuilder;

import java.util.function.Function;

/**
 * @author ALi
 * @version 1.0
 * @date 2023-04-21 10:34
 * @description
 */
public abstract class Parameter<T> {
    protected String name;
    private T defaultValue;

    protected Parameter(String name, T defaultValue) {
        this.name = name;
        this.defaultValue = defaultValue;
    }

    public abstract boolean doesExist();

    public void setValue(T value) {
        if (value != null) {
            save(value);
        }
    }

    protected abstract void save(T value);

    public T getValue(){
        return getValue(defaultValue);
    }

    public abstract T getValue(T defaultValue);

    public T getDefaultValue() {
        return defaultValue;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .append("name", name)
                .append("value", getValue())
                .append("defaultValue", defaultValue)
                .toString();
    }
}
